<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充电宝编号" prop="powerBankNo">
        <el-input
          v-model="queryParams.powerBankNo"
          placeholder="请输入充电宝编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择借用开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借用站点id" prop="startStationId">
        <el-input
          v-model="queryParams.startStationId"
          placeholder="请输入借用站点id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用地点名称" prop="startStationName">
        <el-input
          v-model="queryParams.startStationName"
          placeholder="请输入借用地点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用柜机编号" prop="startCabinetNo">
        <el-input
          v-model="queryParams.startCabinetNo"
          placeholder="请输入借用柜机编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归还时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择归还时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="归还站点id" prop="endStationId">
        <el-input
          v-model="queryParams.endStationId"
          placeholder="请输入归还站点id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归还地点名称" prop="endStationName">
        <el-input
          v-model="queryParams.endStationName"
          placeholder="请输入归还地点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归还柜机编号" prop="endCabinetNo">
        <el-input
          v-model="queryParams.endCabinetNo"
          placeholder="请输入归还柜机编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借用时长" prop="duration">
        <el-input
          v-model="queryParams.duration"
          placeholder="请输入借用时长"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用规则" prop="feeRule">
        <el-input
          v-model="queryParams.feeRule"
          placeholder="请输入费用规则"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用规则id" prop="feeRuleId">
        <el-input
          v-model="queryParams.feeRuleId"
          placeholder="请输入费用规则id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抵扣金额" prop="deductAmount">
        <el-input
          v-model="queryParams.deductAmount"
          placeholder="请输入抵扣金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际订单金额" prop="realAmount">
        <el-input
          v-model="queryParams.realAmount"
          placeholder="请输入实际订单金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信付款时间" prop="payTime">
        <el-date-picker clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择微信付款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="微信支付订单号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入微信支付订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:orderInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:orderInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:orderInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:orderInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="充电宝编号" align="center" prop="powerBankNo" />
      <el-table-column label="借用开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借用站点id" align="center" prop="startStationId" />
      <el-table-column label="借用地点名称" align="center" prop="startStationName" />
      <el-table-column label="借用柜机编号" align="center" prop="startCabinetNo" />
      <el-table-column label="归还时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归还站点id" align="center" prop="endStationId" />
      <el-table-column label="归还地点名称" align="center" prop="endStationName" />
      <el-table-column label="归还柜机编号" align="center" prop="endCabinetNo" />
      <el-table-column label="借用时长" align="center" prop="duration" />
      <el-table-column label="费用规则" align="center" prop="feeRule" />
      <el-table-column label="费用规则id" align="center" prop="feeRuleId" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="抵扣金额" align="center" prop="deductAmount" />
      <el-table-column label="实际订单金额" align="center" prop="realAmount" />
      <el-table-column label="微信付款时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="微信支付订单号" align="center" prop="transactionId" />
      <el-table-column label="订单状态：0:充电中 1：未支付 2：已支付" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:orderInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:orderInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="充电宝编号" prop="powerBankNo">
          <el-input v-model="form.powerBankNo" placeholder="请输入充电宝编号" />
        </el-form-item>
        <el-form-item label="借用开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择借用开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借用站点id" prop="startStationId">
          <el-input v-model="form.startStationId" placeholder="请输入借用站点id" />
        </el-form-item>
        <el-form-item label="借用地点名称" prop="startStationName">
          <el-input v-model="form.startStationName" placeholder="请输入借用地点名称" />
        </el-form-item>
        <el-form-item label="借用柜机编号" prop="startCabinetNo">
          <el-input v-model="form.startCabinetNo" placeholder="请输入借用柜机编号" />
        </el-form-item>
        <el-form-item label="归还时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择归还时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="归还站点id" prop="endStationId">
          <el-input v-model="form.endStationId" placeholder="请输入归还站点id" />
        </el-form-item>
        <el-form-item label="归还地点名称" prop="endStationName">
          <el-input v-model="form.endStationName" placeholder="请输入归还地点名称" />
        </el-form-item>
        <el-form-item label="归还柜机编号" prop="endCabinetNo">
          <el-input v-model="form.endCabinetNo" placeholder="请输入归还柜机编号" />
        </el-form-item>
        <el-form-item label="借用时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入借用时长" />
        </el-form-item>
        <el-form-item label="费用规则" prop="feeRule">
          <el-input v-model="form.feeRule" placeholder="请输入费用规则" />
        </el-form-item>
        <el-form-item label="费用规则id" prop="feeRuleId">
          <el-input v-model="form.feeRuleId" placeholder="请输入费用规则id" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="抵扣金额" prop="deductAmount">
          <el-input v-model="form.deductAmount" placeholder="请输入抵扣金额" />
        </el-form-item>
        <el-form-item label="实际订单金额" prop="realAmount">
          <el-input v-model="form.realAmount" placeholder="请输入实际订单金额" />
        </el-form-item>
        <el-form-item label="微信付款时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择微信付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="微信支付订单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入微信支付订单号" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderInfo">
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo } from "@/api/order/orderInfo";

const { proxy } = getCurrentInstance();

const orderInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    nickname: null,
    orderNo: null,
    powerBankNo: null,
    startTime: null,
    startStationId: null,
    startStationName: null,
    startCabinetNo: null,
    endTime: null,
    endStationId: null,
    endStationName: null,
    endCabinetNo: null,
    duration: null,
    feeRule: null,
    feeRuleId: null,
    totalAmount: null,
    deductAmount: null,
    realAmount: null,
    payTime: null,
    transactionId: null,
    status: null,
  },
  rules: {
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "订单状态：0:充电中 1：未支付 2：已支付不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
    delFlag: [
      { required: true, message: "删除标志不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  listOrderInfo(queryParams.value).then(response => {
    orderInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    nickname: null,
    orderNo: null,
    powerBankNo: null,
    startTime: null,
    startStationId: null,
    startStationName: null,
    startCabinetNo: null,
    endTime: null,
    endStationId: null,
    endStationName: null,
    endCabinetNo: null,
    duration: null,
    feeRule: null,
    feeRuleId: null,
    totalAmount: null,
    deductAmount: null,
    realAmount: null,
    payTime: null,
    transactionId: null,
    status: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null,
    remark: null
  };
  proxy.resetForm("orderInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOrderInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除订单编号为"' + _ids + '"的数据项？').then(function() {
    return delOrderInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/orderInfo/export', {
    ...queryParams.value
  }, `orderInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
